<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Users List</title>
  </head>
  <body>
    <table>
      <tr>
        <td>
          <div id="usersgrid" class="x-form-item">
          </div>
        </td>
        <td>
          <div id="rightsgrid" class="x-form-item">
          </div>
        </td>
      </tr>
    </table>
    
    <div id="chatrecord" class="x-hidden">
    </div>
    <div id="smsrecord" class="x-hidden">
    </div>
    <script>
        /*
         * Ext JS Library 2.1
         * Copyright(c) 2006-2008, Ext JS, LLC.
         * licensing@extjs.com
         *
         * http://extjs.com/license
         */
        var userid;
        var rightGridPanel;
        var rightDS;
        var chatRecordWin;
        var smsRecordWin;
        Ext.onReady(function(){
            //selectedId = -1;//当前选择的数据库ID值
            Ext.QuickTips.init();
            Ext.form.Field.prototype.msgTarget = 'side';
            // fix the bug in codepress - resize
            //Ext.BoxComponent.prototype.setSize('100%', '125px');
                
            /************************************************************
             * Display the result in page
             * the column model has information about grid columns
             * dataIndex maps the column to the specific data field in
             * the data store (created below)
             ************************************************************/
            var userCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                    id: 'id',
                    header: "Identify",
                    dataIndex: 'id',
                    width: 150,
                    hidden: true
                }, {
                    header: "用户名",
                    width: 80,
                    dataIndex: 'user_name'
                }, {
                    header: "密码",
                    width: 80,
                    dataIndex: 'password'
                }, {
                    header: "登录ID",
                    width: 80,
                    dataIndex: 'login_id'
                }, {
                    header: "工号",
                    width: 60,
                    dataIndex: 'employee_no'
                }, {
                    header: "员工类别",
                    width: 60,
                    dataIndex: 'user_type'
                }, {
                    header: "部门",
                    width: 100,
                    dataIndex: 'department_name'
                }, {
                    header: "ET用户名",
                    width: 60,
                    dataIndex: 'eterm_name'
                }, {
                    header: "ET密码",
                    width: 60,
                    dataIndex: 'eterm_pswd'
                }, {
                    header: "是否在线",
                    width: 60,
                    dataIndex: 'is_online'
                }]);
            /************************************************************
             * connect to backend - grid - core part
             * create the Data Store
             *   connect with backend and list the result in page
             *   through JSON format
             ************************************************************/
            var userDS = new Ext.data.Store({
                // load using script tags for cross domain, if the data in on the same domain as
                // this page, an HttpProxy would be better
                proxy: new Ext.data.HttpProxy({
                    url: '/users/getUsers'
                }),
                    
                // create reader that reads the Topic records
                reader: new Ext.data.JsonReader({
                    root: 'root',
                    totalProperty: 'totalProperty',
                    id: 'id'
                }, [{
                        name: 'id'
                    }, {
                        name: 'user_name'
                    }, {
                        name: 'password'
                    }, {
                        name: 'login_id'
                    }, {
                        name: 'employee_no'
                    }, {
                        name: 'user_type'
                    }, {
                        name: 'department_name',
                        mapping: 'department.name'
                    }, {
                        name: 'is_online'
                    }, {
                        name: 'eterm_name'
                    }, {
                        name: 'eterm_pswd'
                    }])
            });
                
            /************************************************************
             * add a paging toolbar to the grid's footer
             ************************************************************/
            var myPagingToolbar = new Ext.PagingToolbar({
                pageSize: 10,
                displayInfo: true,
                displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
                emptyMsg: "没有记录",
                store: userDS
            });
            /************************************************************
             * Define menubar now in here
             *   add and delete functions
             ************************************************************/
            var menubar = [{
                    text: '增加用户',
                    tooltip: '增加用户',
                    iconCls: 'addItem',
                    handler: function(){
                        // add new user now - action
                        CreateUser(userDS);
                    }
                }, '-', {
                    text: '删除用户',
                    tooltip: '删除用户',
                    iconCls: 'remove',
                    handler: function(){
                        // delete user now - action
                        DeleteUser(userDS, userGridPanel);
                    }
                }, '-', {
                    text: '编辑用户',
                    tooltip: '编辑用户',
                    iconCls: 'editItem',
                    handler: function(){
                        // delete user now - action
                        EditUser(userDS, userGridPanel);
                    }
                }, '-', {
                    text: '用户消息',
                    tooltip: '用户消息',
                    iconCls: 'recorder',
                    handler: function(){
                        // delete user now - action
                        getChatRecordList(userDS, userGridPanel);
                    }
                }, '-', {
                    text: '短信记录',
                    tooltip: '短信记录',
                    iconCls: 'recorder',
                    handler: function(){
                        // delete user now - action
                        getSMSRecordList(userDS, userGridPanel);
                    }
                }];
                
            /************************************************************
             * define grid panel now
             ************************************************************/
            var userGridPanel = new Ext.grid.GridPanel({
                border: false,
                el: 'usersgrid',
                ds: userDS,
                cm: userCM,
                width:700,
                height:600,
                collapsible: true,
                animCollapse: false,
                title: '员工列表',
                loadMask: true,
                tbar: menubar,
                bbar: myPagingToolbar
            });
                
            //panel render
            userGridPanel.render();
            // trigger the data store load
            userDS.load({
                params: {
                    start: 0,
                    limit: 20
                }
            });
            /************************************************************
             * Action - edit
             *   handle double click
             *   user select one of the item and want to update it
             ************************************************************/
            userGridPanel.on('rowdblclick', function(userGridPanel, rowIndex, e){
                userid = userDS.data.items[rowIndex].id;
                rightDS.baseParams.user_id = userid;
                rightDS.load({callback: function(record, options, success){  
                        if(success){  
                            //Ext.Msg.alert('操作','成功!!！');
                            showUserRight();                            
                        }else{  
                            Ext.Msg.alert('操作','失败！');  
                        }
                    }});
            });
            //showUserRight
            /*************************************************************
             * 用户所属权限列表
             ***********************************************************/
            var rightSM = new Ext.grid.CheckboxSelectionModel();
            var rightCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), rightSM, {
                    id: 'id',
                    header: "Identify",
                    dataIndex: 'id',
                    width: 150,
                    hidden: true
                }
                /*, {
                    header: "ControllerName",
                    width: 100,
                    dataIndex: 'controller_name'
                }, {
                    header: "ActionName",
                    width: 100,
                    dataIndex: 'action_name'
                }*/
                , {
                    header: "描述",
                    width: 100,
                    dataIndex: 'description'
                }]);
                
            /************************************************************
             * connect to backend - grid - core part
             * create the Data Store
             *   connect with backend and list the result in page
             *   through JSON format
             ************************************************************/
            rightDS = new Ext.data.Store({
                // load using script tags for cross domain, if the data in on the same domain as
                // this page, an HttpProxy would be better
                proxy: new Ext.data.HttpProxy({
                    url: '/rights/getRights'
                }),
                    
                // create reader that reads the Topic records
                reader: new Ext.data.JsonReader({
                    root: 'root',
                    totalProperty: 'totalProperty',
                    id: 'id'
                }, [{
                        name: 'id'
                    }, {
                        name: 'controller_name'
                    }, {
                        name: 'action_name'
                    }, {
                        name: 'description'
                    }])
            });
                
            /************************************************************
             * Define menubar now in here
             *   add and delete functions
             ************************************************************/
            var menubar = [{
                    text: '保存用户权限',
                    tooltip: '保存用户权限',
                    iconCls: 'save',
                    handler: function(){
                        SaveUserRight(rightDS, rightGridPanel);
                    }
                }];
            /************************************************************
             * define grid panel now
             ************************************************************/
            rightGridPanel = new Ext.grid.GridPanel({
                border: false,
                el: 'rightsgrid',
                ds: rightDS,
                cm: rightCM,
                sm: rightSM,
                width:150,
                height:600,
                collapsible: true,
                animCollapse: false,
                title: '权限列表',
                loadMask: true,
                tbar: menubar
            });
            //register afterload event for check
            //                rightGridPanel.getStore().on('afterload', function(){
            //                    Ext.MessageBox.alert('Confirm', '提交成功');
            //                });
            /*rightGridPanel.getStore().on({
                load: function(){//beforeload
                    //this.prevScroll = this.getScrollState();
                    //this.prevScroll.top = 0;
                    //Ext.MessageBox.alert('消息', userid);
                    Ext.Ajax.request({
                        url: '/rights/getRightsByUserID?user_id=' + userid,
                        //params: loginForm.form.getValues(true),
                        method: 'GET',
                        success: function(result, request){
                            //var reader = new Ext.data.JsonReader(result.responseText)
                            //Ext.MessageBox.alert('消息', rightDS.data.items[0].data.controller_name);
                            var rights = rightDS.data.items;
                            var rightids = result.responseText.split(',');
                            var rowsArray = new Array();
                            //Ext.MessageBox.alert('消息', result.responseText.split(','));
                            if (rightids.length > 0) {
                                for (i = 0; i < rightids.length; i++) {
                                    for (j = 0; j < rights.length; j++) {
                                        //var xx=rights[j].data.id;
                                        //var yy=rightids[i];
                                        if (rights[j].data.id == rightids[i]) {
                                            rowsArray.push(j);
                                            break;
                                            //rightSM.selectRow(j);
                                        }
                                    }
                                }
                                //Ext.MessageBox.alert('消息', rightids[0]+":"+result.responseText);
                            }
                            //Ext.MessageBox.alert('消息', rowsArray);
                            rightSM.selectRows(rowsArray);
                            //
                        },
                        failure: function(result, request){
                            Ext.MessageBox.alert('提示', '取得用户对应权限时出错!');
                        }
                    });
                }
            });*/
            //panel render
            rightGridPanel.render();
                
            function showUserRight(){
                Ext.Ajax.request({
                    url: '/rights/getRightsByUserID?user_id=' + userid,
                    //params: loginForm.form.getValues(true),
                    method: 'GET',
                    success: function(result, request){
                        //var reader = new Ext.data.JsonReader(result.responseText)
                        //Ext.MessageBox.alert('消息', rightDS.data.items[0].data.controller_name);
                        var rights = rightDS.data.items;
                        var rightids = result.responseText.split(',');
                        var rowsArray = new Array();
                        //Ext.MessageBox.alert('消息', result.responseText.split(','));
                        if (rightids.length > 0) {
                            for (i = 0; i < rightids.length; i++) {
                                for (j = 0; j < rights.length; j++) {
                                    //var xx=rights[j].data.id;
                                    //var yy=rightids[i];
                                    if (rights[j].data.id == rightids[i]) {
                                        rowsArray.push(j);
                                        break;
                                        //rightSM.selectRow(j);
                                    }
                                }
                            }
                            //Ext.MessageBox.alert('消息', rightids[0]+":"+result.responseText);
                        }
                        //Ext.MessageBox.alert('消息', rowsArray);
                        rightSM.selectRows(rowsArray);
                        //
                    },
                    failure: function(result, request){
                        Ext.MessageBox.alert('提示', '取得用户对应权限时出错!');
                    }
                });
            }      

            //save right for user
            function SaveUserRight(dataStore, rightGridPanel){
                //var records = rightGridPanel.getSelectionModel().getSelections();
                // get the selected items
                var m = rightGridPanel.getSelections();
                if (m.length > 0) {
                    Ext.MessageBox.confirm('提示', '你真的要保存这些用户的权限吗？', function(btn){
                        if (btn == 'yes') {
                            var jsonData = "[";
                            for (var i = 0, len = m.length; i < len; i++) {
                                var ss = "{\"id\":\"" + m[i].get("id") + "\"}";
                                if (i == 0) 
                                    jsonData = jsonData + ss;
                                else jsonData = jsonData + "," + ss;
                                rightDS.remove(m[i]);
                            }
                            jsonData = jsonData + "]";
                            //Ext.MessageBox.alert(jsonData);
                            rightDS.reload({
                                params: {
                                    saveData: jsonData,
                                    user_id: userid
                                },callback: function(record, options, success){  
                                    if(success){  
                                        //Ext.Msg.alert('操作','成功!!！');
                                        showUserRight();                            
                                    }else{  
                                        Ext.Msg.alert('操作','失败！');  
                                    }
                                }
                            });
                        }
                    });
                }
                else {
                    rightDS.reload({
                        params: {
                            saveData: '0',
                            user_id: userid
                        },callback: function(record, options, success){  
                            if(success){  
                                //Ext.Msg.alert('操作','成功!!！');
                                showUserRight();                            
                            }else{  
                                Ext.Msg.alert('操作','失败！');  
                            }
                        }
                    });
                }
            }
        });
    </script>
  </body>
</html>
